package servlet;

import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.EmployeeDAO;
import dao.FeedbackDAO;
import dao.ObjectDAO;

import model.Employee;
import model.Feedback;
import model.JobRes;

/**
 * Servlet implementation class feedbackAction
 */
public class feedbackAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
    ObjectDAO oDao;
    FeedbackDAO fDao;
    EmployeeDAO emDao;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public feedbackAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		ArrayList<JobRes> list = new ArrayList<JobRes>();
		
		try {
			oDao = new ObjectDAO();
			fDao = new FeedbackDAO();
			emDao = new EmployeeDAO();
			
			Employee em = (Employee) session.getAttribute("person");
			
			if(!session.isNew()) {
				String d = request.getParameter("date_picker");
				Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(d);
				
				String content = request.getParameter("content");
				int user = Integer.parseInt(request.getParameter("user"));
				
				Employee target = new Employee();
				int emId = 0;
				if(user == 2) {
					String temp = request.getParameter("search");
					emId = Integer.parseInt(temp);
					target = emDao.getAEmployee(emId);
				}
				else {
					target = (Employee) session.getAttribute("person");
				}
				
				int sec = Integer.parseInt(request.getParameter("section"));
				
				Feedback fb = new Feedback(0, content, em, target, date, null, oDao.getASection(sec));
				System.out.println(fDao.addFeedback(fb));
				
				response.sendRedirect("viewFeed.jsp");
			}
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
	}

}
